package com.lishe.lishe.lishe.module.excitation.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.module.excitation.bean.AchievementObject;
import com.lishe.lishe.lishe.utils.TimeUtils;

/**
 * Created by DELL on 2017/10/30.
 */

public class AchievementGiveTimeDialog extends Dialog {


    public AchievementGiveTimeDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    public static class Builder implements View.OnClickListener {
        private Activity context;
        private AchievementObject achievement;
        private AchievementGiveTimeDialog dialog;

        public Builder(Activity context) {
            this.context = context;
        }

        public Builder setAchievementObject(AchievementObject achievement) {
            this.achievement = achievement;
            return this;
        }


        public AchievementGiveTimeDialog create() throws NullPointerException {
            if (dialog == null) {
                dialog = new AchievementGiveTimeDialog(context, R.style.MyDialog);
                View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_ahievement_give_time, null);
                ImageView img = contentView.findViewById(R.id.img);
                ImageView dissmiss = contentView.findViewById(R.id.dissmiss);
                TextView text = contentView.findViewById(R.id.text);
                TextView time = contentView.findViewById(R.id.time);

                ImageLoaderUtils.displayImage(context,achievement.getAchieveImg(), img,R.mipmap.dialog_achievement_get_success);
//                if (achievement.getAchieveType().equals("1") || achievement.getAchieveType().equals("4")) {//1:基础成就 4:行为成就
                text.setText(achievement.getAchieveDesc());
//                } else {
//                    text.setText("获得" + achievement.getAchieveName() + "行为" + achievement.getAdmireCount() + "个赞");
//                }
                if (achievement.getAchievedTime() == 0) {
                    time.setText("null");
                } else {
                    time.setText(TimeUtils.longToyyyyMMdd(achievement.getAchievedTime()) + "获得");
                }

                dissmiss.setOnClickListener(this);
                contentView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        dialog.cancel();
                    }
                });

                dialog.setContentView(contentView);
                dialog.getWindow().setGravity(Gravity.CENTER);
                dialog.setCanceledOnTouchOutside(true);
                dialog.setCancelable(true);
                WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
                lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                lp.height = WindowManager.LayoutParams.MATCH_PARENT;
                dialog.getWindow().setAttributes(lp);
            }
            return dialog;
        }

        public void show() {
            try {
                create();
                if (dialog != null && !dialog.isShowing()) {
                    dialog.show();
                }
            } catch (NullPointerException e) {
                e.printStackTrace();
            }
        }

        public void dissmiss() {
            dialog.dismiss();
            dialog.cancel();
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.dissmiss:
                case R.id.sure:
                    dissmiss();
                    break;
            }
        }
    }

}
